#!/bin/bash

LOGFILE=output.log
UT_TEST_DIR=unit_test;
UT_SCRIPT_FILE_NAME=unit_test.sh;
UT_PACKAGE_NAME=unit_test.tgz
SOURCE_FILE_PATH="Framework/utest lcd_application/utest";

PACK_DEP_MODULE_LIST="gmock"
UT_FILE_LIST="Framework/lib/liblcdappframework.so Framework/utest/lcd_app_frame_work_unit_test lcd_application/utest/lcdappd_unit_test unit_test.sh";


WriteLog()
{
	echo "`date +%Y%m%d_%T`  $*" >> "$LOGFILE";
	echo "$*";
}

BuildUT()
{
	WriteLog "******Start to Build Unit test....******";
	for UT_SRC in ${SOURCE_FILE_PATH}
	do
		if [ -e ${UT_SRC} -a -d ${UT_SRC} ]
		then
			# CURRENT_PATH=`pwd`;
			cd ${UT_SRC};
       		make clean >> "$LOGFILE" 2>&1;
			make  >> "$LOGFILE" 2>&1;
			if [ $? -ne 0 ]
			then
				WriteLog "Some thing exception happen when compile the [${UT_SRC}] code,please check the logfile[${LOGFILE}]"
				exit 1
			else
				WriteLog "Compile ut code [${UT_SRC}] successful"
			fi
	        cd -
		else
			WriteLog "The source file dir [`pwd`${UT_SRC}] does not exists,please check the install package";
       		exit 2
		fi
	done
	WriteLog "******Build Unit test Finish....******";
}

CreateUTPackage()
{
	WriteLog "******Start to Pack [${UT_PACKAGE_NAME}]....******";
	
	#pack PACK_DEP_MODULE_LIST
	for module in ${PACK_DEP_MODULE_LIST}
	do 
		cp -rf ${module}/* ${UT_TEST_DIR} >>${LOGFILE} 2>&1;
		#find ./ -name "${module}" -exec tar -zxvf {} -C ${UT_TEST_DIR}/ \; >>${LOGFILE} 2>&1;
		if [ $? -ne 0 ]
		then
			WriteLog "proc module file [ ${module} ] failed,errorcode is $?"
			exit 4;
		fi
	done
	#pack executeable file and cfg 	
	for file in ${UT_FILE_LIST}
    do	
		cp -rf ${file} ${UT_TEST_DIR} >>${LOGFILE} 2>&1;
		if [ $? -ne 0 ]
		then
			WriteLog "Copy file [ ${file} ] to ${UT_TEST_DIR} failed,errorcode is $?"
			exit 5;
		fi
	done
	#rename the script file
    # mv -f ${SOURCE_FILE_PATH}/unit_test.sh   ${UT_TEST_DIR}/${UT_SCRIPT_FILE_NAME};
    cp -f ./unit_test.sh   ${UT_TEST_DIR}/${UT_SCRIPT_FILE_NAME};
	if [ $? -ne 0 ]
	then
		WriteLog "Rename [${UT_TEST_DIR}/unit_test.sh ] to [${UT_TEST_DIR}/${UT_SCRIPT_FILE_NAME}] failed,errorcode is $?"
		exit 6;
	fi 

	chmod u+x ${UT_TEST_DIR}/${UT_SCRIPT_FILE_NAME};

	tar zcf ${UT_PACKAGE_NAME} --group=root --owner=root ${UT_TEST_DIR} >>${LOGFILE} 2>&1;
	if [ $? -ne 0 ]
	then
		WriteLog "create ${UT_PACKAGE_NAME} failed,errorcode is $?";
		exit 7;
	fi

	WriteLog "******Pack [${UT_PACKAGE_NAME}] Finish....******";
	return 0;	
}

if [ $# -lt 3 ]
then
	echo -e "Useage:$0 ut_script_file_name test_package_dir_name log_file_name\nExample:\n\t$0 unit_test.sh /root/unit_test /root/output.log\n";
	echo -e "\t$0 unit_test.sh unit_test output.log\n";
#	exit 8;
	UT_SCRIPT_FILE_NAME="unit_test.sh"
	UT_TEST_DIR="unit_test"
	LOGFILE="`pwd`/output.log"
else
	UT_SCRIPT_FILE_NAME=$1;
	UT_TEST_DIR=$2;
	LOGFILE=$3;
fi
	if [ -d ${UT_TEST_DIR} ]
	then
		rm -rf ${UT_TEST_DIR};
	fi
	mkdir -p ${UT_TEST_DIR};
	if [ $? -ne 0 ]
	then
		WriteLog "Create dir[${UT_TEST_DIR}] failed,errorcode is [$?]";
		exit 9;
	fi
	UT_PACKAGE_NAME="`basename ${UT_TEST_DIR}`.tgz";
	rm -f ${UT_PACKAGE_NAME};

BuildUT;
CreateUTPackage;
exit 0;
